#include "graph.h"
#include "ui_graph.h"

graph::graph(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::graph)
{
    ui->setupUi(this);
    init();
}

void graph::updateGraph(int x,int y)
{
    ui->graphWidget->graph(0)->addData(x,y);      //温度加到数据中
    ui->graphWidget->graph(1)->addData(x,y);
    ui->graphWidget->xAxis->setRange(x+6,20, Qt::AlignRight);  //设置x轴范围
    ui->graphWidget->replot();
}
void graph::init()
{
    //绘图初始化
    // 添加曲线 1
//    ui->graphWidget->resize(400,230);
    ui->graphWidget->addGraph();         // 增加图层
    ui->graphWidget->graph(0)->setPen(QPen(Qt::red));
    ui->graphWidget->graph(0)->setBrush(QBrush(QColor(0,255,0,66)));     // 设置图层画刷颜色
    ui->graphWidget->graph(0)->setAntialiasedFill(true);        // 设置图层反锯齿
    // 添加点
    ui->graphWidget->addGraph();
    ui->graphWidget->graph(1)->setPen(QPen(Qt::blue,1));       // 设置笔的颜色
    ui->graphWidget->graph(1)->setLineStyle(QCPGraph::lsNone);   // 不画线条
    ui->graphWidget->graph(1)->setScatterStyle(QCPScatterStyle::ssNone);     // 设置点的形状

    //坐标轴1设置
    QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
    timeTicker->setTimeFormat("%m:%s");             //时间格式：  秒
    ui->graphWidget->xAxis->setTicker(timeTicker);  // 横坐标显示时间
    ui->graphWidget->axisRect()->setupFullAxesBox();
    ui->graphWidget->axisRect()->setupFullAxesBox();     //坐标为矩
    // x轴设置
    ui->graphWidget->xAxis->setLabel("时间(单位s)");         // 设置横轴标签
    ui->graphWidget->yAxis->setRange(0,15);       //设置Y轴范围
    ui->graphWidget->yAxis->setLabel("FPS");
    //ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);    //设置属性可缩放，移动
    ui->graphWidget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);    //允许可缩放，移动
    QList < QCPAxis *>  xis;
    xis.append(ui->graphWidget->xAxis);
    ui->graphWidget->axisRect()->setRangeZoomAxes(xis);

    // 轴随动
    connect(ui->graphWidget->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->graphWidget->xAxis2, SLOT(setRange(QCPRange)));
    connect(ui->graphWidget->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->graphWidget->yAxis2, SLOT(setRange(QCPRange)));

}
graph::~graph()
{
    delete ui;
}
